<template>
  <view class="content">
    <view class="title">子组件B</view>
    <view class="b-box">
      CompA组件传进来的值：
      <!-- 展示从A接收的值 -->
      <text class="intent-text-box">{{ aValueFromA }}</text>
    </view>
    <view class="b-box" style="margin: 10rpx;">
      <text>回传值：</text>
      <input type="text" style="color: yellow;" v-model="bvalue" />
      <button size="mini" @click="sentOut()">回传</button>
    </view>
  </view>
</template>

<script>
export default {
  name: "CompB",
  data() {
    return {
      bvalue: "",
      aValueFromA: "" // 新增：存储从A接收的值
    };
  },
  created() {
    console.log("created comB");
    // 监听A组件触发的全局事件「fasong」，接收值并赋值
    uni.$on("fasong", (msg) => {
      console.log("COMB 接收A的值：" + msg);
      this.aValueFromA = msg; // 赋值后页面自动渲染
    });
  },
  // 重要：组件销毁时移除全局事件监听，避免内存泄漏
  beforeDestroy() {
    uni.$off("fasong");
  },
  mounted() {
    console.log("mounted comB");
  },
  methods: {
    sentOut() {
      console.log('comp B sentOut');
      this.$emit('jiantingqi', this.bvalue);
    }
  }
};
</script>

<style>
.content {
  height: 300rpx;
  margin: 50rpx;
  background-color: #99CCFF;
}
.title {
  font-size: 50rpx;
  text-align: center;
}
.intent-text-box {
  font-weight: bold;
  color: red;
  padding-left: 20rpx;
  padding-right: 20rpx;
}
.b-box {
  height: 80rpx;
  display: flex;
  flex-direction: row;
  align-items: center;
  padding-left: 20rpx;
}
input {
  width: 250rpx;
  background-color: #8f8f94;
}
</style>